package service.impl;

import dao.StuScoreMMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.StuScoreM;
import service.StuScoreMService;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class StuScoreMServiceImpl implements StuScoreMService {

    @Autowired
    StuScoreMMapper stuScoreMMapper;

    @Override
    public List<StuScoreM> getAll(String name) {
        return stuScoreMMapper.getAll("%"+name+"%");
    }

    @Override
    public StuScoreM getOne(String id) {
        return stuScoreMMapper.getOne(id);
    }

    @Override
    public int addScore(StuScoreM stuScoreM) {
        log.info("stuScoreM="+stuScoreM);
        if(stuScoreM.getId() == null){
            log.warn("stuScoreM缺少id,随机生成中");
            stuScoreM.setId(UUID.randomUUID().toString());
        }
        return stuScoreMMapper.addScore(stuScoreM);
    }

    @Override
    public int updateScore(StuScoreM stuScoreM) {
        return stuScoreMMapper.updateScore(stuScoreM);
    }

    @Override
    public int deleteScore(String id) {
        return stuScoreMMapper.deleteScore(id);
    }

    @Override
    public List<StuScoreM> getStuScoreByArray(Integer currPage, Integer pageSize) {
        List<StuScoreM> stuScoreMS = stuScoreMMapper.getAllByArray();
        int firstIndex = (currPage -1 )*pageSize;
        int lastIndex = currPage * pageSize;
        if((stuScoreMS.size()-firstIndex)<pageSize){
            stuScoreMS =stuScoreMS.subList(firstIndex,stuScoreMS.size());
        }
        try{
            stuScoreMS = stuScoreMS.subList(firstIndex,lastIndex);
        }catch (Exception e){
            e.printStackTrace();
        }
        return stuScoreMS;
    }

    @Override
    public List<StuScoreM> getAll() {
        List<StuScoreM> stuScoreMS = stuScoreMMapper.getAllByArray();

       return stuScoreMS;
    }
}
